﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using MyGeneration.dOOdads;
using MethodIT.Helper;

using Hr20.DAL;
using Hr20.BLL;
using Hr20.ProcessFlow;

namespace Appraisal.Web.ZB
{
    public partial class ChooseTender : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                int nSubID = HelperURL.GetIntFromUrlString("SubID");

                KP_Subject vw = new KP_Subject();
                if (vw.LoadByPrimaryKey(nSubID))
                {
                    labSubjectName.Text = vw.SubjectName;
                }
                LoadData(nSubID);
            }
        }

        private void LoadData(int SubID)
        {

            string Selected = "0";
            vw_Subject_Tender vw = new vw_Subject_Tender();
            vw.Where.SubID.Value = SubID;
            vw.Query.AddOrderBy(vw_Subject_Tender.ColumnNames.TenderName, WhereParameter.Dir.ASC);
            if (vw.Query.Load())
            {
                do
                {
                    Selected += vw.TenderID + ",";
                }
                while (vw.MoveNext());
                //Selected.TrimEnd(',');
                Selected += "0";
            }
            //排除已选择的人员
            ZB_Tender t = new ZB_Tender();
            t.Where.Enable.Value = "Y";
            t.Where.ID.Value = Selected;
            t.Where.ID.Value =  Selected;
            t.Where.ID.Operator = WhereParameter.Operand.NotIn;
            if (txtName.Text != "")
            {
                t.Where.FullName.Value = "%" + txtName.Text + "%";
                t.Where.FullName.Operator = WhereParameter.Operand.Like;
            }
            t.Query.Load();
            dg.DataSource = t.DefaultView;
            dg.DataBind();
        }

        protected void lbtnSave_Click(object sender, EventArgs e)
        {
            doSave();
        }

        private void doSave()
        {
            int SubID = HelperURL.GetIntFromUrlString("SubID");
            int OrderID = 0;
            KP_Subject_Employee se = new KP_Subject_Employee();
            se.Where.SID.Value = SubID;
            se.Query.AddOrderBy(KP_Subject_Employee.ColumnNames.OrderID, MyGeneration.dOOdads.WhereParameter.Dir.DESC);
            if (se.Query.Load())
            {
                OrderID = se.OrderID;
            }

            se = new KP_Subject_Employee();
            OrderID += 1;

            foreach (DataGridItem dgi in dg.Items)
            {
                if (dgi.ItemType == ListItemType.Item || dgi.ItemType == ListItemType.AlternatingItem)
                {
                    CheckBox chkSelectPending = (CheckBox)dgi.FindControl("chkSelectPending");
                    if (chkSelectPending.Checked)
                    {
                        int SeID = Int32.Parse(dgi.Cells[1].Text);
                        se.AddNew();
                        se.SID = SubID;
                        se.EmployeeID = SeID;
                        se.Total = 0;
                        se.OrderID = OrderID ++ ;
                    }
                }
            }
            TransactionMgr tx = TransactionMgr.ThreadTransactionMgr();
            try
            {
                tx.BeginTransaction();
                se.Save();
                tx.CommitTransaction();
                GoBack();
            }
            catch
            {
                tx.RollbackTransaction();
                TransactionMgr.ThreadTransactionMgrReset();
            }
        }

        private void GoBack()
        {
            int SubID = HelperURL.GetIntFromUrlString("SubID");
            string URL = string.Format("../Appraisal/SubjectApEmployee.aspx?ID={0}", SubID);
            HelperURL.Redirect(URL);
        }

        protected void lbtnBack_Click(object sender, EventArgs e)
        {
            GoBack();
        }

        protected void lbtnMakeChoose_Click(object sender, EventArgs e)
        {
            int nSubID = HelperURL.GetIntFromUrlString("SubID");
            LoadData(nSubID);
        }
    }
}
